package socdev.data;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

public class Task{

	public String description;
	public String name;
	public String key;
	public Date deadline;
	
	public Task(String name, String description, Date deadline, String key) {
		this.name = name;
		this.description = description;
		this.key = key;
		this.deadline = deadline;
	}
	
	public Task(String name, String description, Date deadline) {
		this(name, description, deadline, null);
	}
	
	public static Task createFromJson(JSONObject json) throws JSONException {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); //please notice the capital M
		Date date;
		try {
			date = formatter.parse(json.getString("deadline"));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			date = null;
		}
		
		return new Task(json.getString("name"), json.getString("description"), date, json.getString("key"));
	}
	
	public String getId() {
		return key;
	}
	

	public List<NameValuePair> getCreatNameValuePairs(List<NameValuePair> nameValuePairs) {
		 
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
		nameValuePairs.add(new BasicNameValuePair("description",description));
		nameValuePairs.add(new BasicNameValuePair("deadline",dateformat.format(deadline)));
		nameValuePairs.add(new BasicNameValuePair("name",name));
     	return nameValuePairs;
		
	}
	
}
